/**
 * Created by ebi on 2017/4/20.
 */
import api from './api'

export default {
  data () {
    return {
      environment: 'develop', // develop || production
      prePath: '',
      loading: false
    }
  },
  created () {
    this.prePath = (this.environment === 'develop' ? '' : '')
  },
  methods: {
    request (opt) {
      const self = this
      if (opt.act) {
        api.forEach(item => {
          if (item.act === opt.act) {
            opt.url = item.url
            if (item.method) {
              opt.method = item.method
            }
          }
        })
      }
      const authorization = self.getCookie('token') || 'A27B063D2B8C4216A396958E8B1B57E5'
      const defaultOption = {
        method: 'get',
        url: '',
        body: {},
        headers: { 'Authorization': authorization },
        timeout: 20000,
        emulateJSON: true
      }
      const option = Object.assign(defaultOption, opt)
      if (option.method.toLowerCase() === 'get') {
        let paramFlag = false
        for (let i in option.body) {
          if (option.url.indexOf('?') < 0 && !paramFlag) {
            option.url += '?' + i + '=' + option.body[i]
            paramFlag = true
          } else {
            option.url += '&' + i + '=' + option.body[i]
          }
        }
      }
      return new Promise((resolve, reject) => {
        this.$http(option).then(res => {
          if (res.ok) {
            if (res.data.code === 1000) { // 未登录
              self.$router.push('/login?back=' + self.$route.fullPath)
            } else if (res.data.code === 1002) { // 无权限
                            /* eslint-disable*/
                            layer.open({ content: '你没有权限进行此操作，如需开权限，请联系管理员', btn: ['好的'] })
                        }
                        resolve(res.data)
                    } else {
                        layer.open({
                            content: res.status + ' ' + res.statusText + ' : ' + res.url,
                            btn: ['好的'],
                            yes: function() {
                                layer.closeAll()
                            }
                        })
                    }
                }, (e) => {
                    layer.open({
                        content: e.status + ' ' + e.statusText + ' : ' + e.url,
                        btn: ['好的'],
                        yes: function() {
                            layer.closeAll()
                        }
                    })
                    reject(e)
                })
            })
        },
        getCookie(key) {
            let result = ''
            let arr = document.cookie.match(new RegExp('(^| )' + key + '=([^;]*)(;|$)'))
            if (arr != null) {
                try {
                    result = decodeURIComponent(arr[2])
                } catch (err) {
                    result = unescape(arr[2])
                }
            }
            return result
        },
        refuse() {
            const self = this
            layer.open({
                content: '确定要拒绝该用户申请九机白条吗？',
                btn: ['确定', '取消'],
                yes: function() {
                    layer.open({ type: 2, shadeClose: false })
                    self.request({
                        act: 'refuse',
                        body: { platUserId: self.userId }
                    }).then(res => {
                        layer.closeAll()
                        if (res.code == 0) {
                            layer.open({ content: res.userMsg, skin: 'msg', time: 2 })
                            setTimeout(() => {
                                self.$router.replace('/detail/' + self.userId)
                            }, 2000)
                        } else {
                            layer.open({ content: res.userMsg, skin: 'msg', time: 2 })
                        }
                    })
                }
            })
        }
    }
}